﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models.world {
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class MissionModel extends Actor {

        private var _defaultMission:MissionVO;
        private var _levelMissions:Array;
        private var _allMissions:Array;
        private var _pendingMissionCreate:Boolean;

        public function MissionModel(){
            this._levelMissions = [];
            this._allMissions = [];
            super();
        }
        public function addDefaultMission(_arg1:Object, _arg2:Boolean=false):void{
            var _local4:TaskVO;
            var _local5:Object;
            var _local3:MissionVO = new MissionVO(_arg1.id, _arg1.code, _arg1.coins, _arg1.xp, false);
            _local3.isNew = _arg2;
            for each (_local5 in _arg1.tasks) {
                _local4 = new TaskVO(_local5.id, _local5.action, _local5.type, _local5.current, _local5.required);
                _local3.addTask(_local4);
            };
            this._defaultMission = _local3;
            this._allMissions.splice(0, 0, _local3);
            dispatch(new InfoEvent(EventConstants.ADD_MISSION, null, _local3));
            this.checkForUnCompletedMission();
        }
        public function addLevelMissions(_arg1:Array, _arg2:Boolean=false):void{
            var _local3:Object;
            var _local4:MissionVO;
            var _local5:TaskVO;
            var _local6:Object;
            if (((!(_arg1)) || ((_arg1.length == 0)))){
                return;
            };
            for each (_local3 in _arg1) {
                _local4 = new MissionVO(_local3.id, _local3.code, _local3.coins, _local3.xp, true);
                _local4.isNew = _arg2;
                for each (_local6 in _local3.tasks) {
                    _local5 = new TaskVO(_local6.id, _local6.action, _local6.type, _local6.current, _local6.required);
                    _local4.addTask(_local5);
                };
                this._levelMissions.splice(0, 0, _local4);
                this._allMissions.splice(0, 0, _local4);
                dispatch(new InfoEvent(EventConstants.ADD_MISSION, null, _local4));
            };
        }
        public function getMissionById(_arg1:String):MissionVO{
            var _local2:MissionVO;
            for each (_local2 in this._allMissions) {
                if (_local2.id == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        public function removeMission(_arg1:String):MissionVO{
            var _local2:MissionVO;
            var _local3:int;
            _local3 = 0;
            while (_local3 < this._allMissions.length) {
                _local2 = this._allMissions[_local3];
                if (_local2.id == _arg1){
                    this._allMissions.splice(_local3, 1);
                    dispatch(new InfoEvent(EventConstants.REMOVE_MISSION, null, _local2));
                };
                _local3++;
            };
            if (this.defaultMission.id == _arg1){
                _local2 = this.defaultMission;
                this._defaultMission = null;
                return (_local2);
            };
            _local3 = 0;
            while (_local3 < this._levelMissions.length) {
                _local2 = this._levelMissions[_local3];
                if (_local2.id == _arg1){
                    this._levelMissions.splice(_local3, 1);
                    return (_local2);
                };
                _local3++;
            };
            return (null);
        }
        public function get defaultMission():MissionVO{
            return (((this._defaultMission) || (new MissionVO("", "", 0, 0, false))));
        }
        public function getAffectedTaskId(_arg1:String, _arg2:String):String{
            var _local3:TaskVO = this.getAffectedTask(_arg1, _arg2);
            if (_local3){
                return (_local3.id);
            };
            return (null);
        }
        public function getAffectedTaskIds(_arg1:String, _arg2:String):String{
            var _local5:TaskVO;
            var _local3:Array = this.getAffectedTasks(_arg1, _arg2);
            if (_local3.length == 0){
                return (null);
            };
            var _local4 = "";
            for each (_local5 in _local3) {
                _local4 = (_local4 + (_local5.id + ","));
            };
            _local4 = _local4.slice(0, _local4.lastIndexOf(","));
            return (_local4);
        }
        private function getAffectedTasks(_arg1:String, _arg2:String):Array{
            var _local3:TaskVO;
            var _local5:MissionVO;
            var _local4:Array = [];
            for each (_local5 in this._allMissions) {
                _local3 = this.getAffectedTaskImpl(_local5, _arg1, _arg2);
                if (_local3){
                    _local4.push(_local3);
                };
            };
            return (_local4);
        }
        private function getAffectedTask(_arg1:String, _arg2:String):TaskVO{
            var _local3:TaskVO;
            var _local4:MissionVO;
            for each (_local4 in this._allMissions) {
                _local3 = this.getAffectedTaskImpl(_local4, _arg1, _arg2);
                if (_local3){
                    return (_local3);
                };
            };
            return (null);
        }
        private function getAffectedTaskImpl(_arg1:MissionVO, _arg2:String, _arg3:String):TaskVO{
            var _local4:TaskVO;
            var _local5:Boolean;
            for each (_local4 in _arg1.tasks) {
                _local5 = (((_local4.type == _arg3)) || ((_local4.type == GameConstants.ANY_TYPE)));
                if (((((!(_local4.isFinished)) && ((_local4.action == _arg2)))) && (_local5))){
                    return (_local4);
                };
            };
            return (null);
        }
        public function incrementAffectedTask(_arg1:String, _arg2:String):void{
            var _local4:TaskVO;
            var _local3:Array = this.getAffectedTasks(_arg1, _arg2);
            if (_local3.length == 0){
                return;
            };
            for each (_local4 in _local3) {
                this.incrementTask(_local4);
            };
        }
        private function getMissionByTask(_arg1:TaskVO):MissionVO{
            var _local2:MissionVO;
            for each (_local2 in this._allMissions) {
                if (this.containsTask(_local2, _arg1.id)){
                    return (_local2);
                };
            };
            return (null);
        }
        private function containsTask(_arg1:MissionVO, _arg2:String):Boolean{
            var _local3:TaskVO;
            for each (_local3 in _arg1.tasks) {
                if (_local3.id == _arg2){
                    return (true);
                };
            };
            return (false);
        }
        private function checkForUnCompletedMission():void{
            var _local1:MissionVO;
            for each (_local1 in this._allMissions) {
                if (this.isMissionSolved(_local1)){
                    dispatch(new InfoEvent(EventConstants.COLLECT_MISSION_REWARD, _local1.id, _local1.code));
                };
            };
        }
        private function isMissionSolved(_arg1:MissionVO):Boolean{
            var _local2:TaskVO;
            if (!_arg1.isSolved){
                for each (_local2 in _arg1.tasks) {
                    if (_local2.current < _local2.required){
                        return (false);
                    };
                };
            };
            return (true);
        }
        private function incrementTask(_arg1:TaskVO):void{
            var _local4:TaskVO;
            _arg1.current++;
            var _local2:MissionVO = this.getMissionByTask(_arg1);
            var _local3:Boolean;
            for each (_local4 in _local2.tasks) {
                if (!_local4.isFinished){
                    _local3 = false;
                };
            };
            if (_local3){
                dispatch(new InfoEvent(EventConstants.ON_SOLVED_MISSION, _local2.id));
            } else {
                dispatch(new InfoEvent(EventConstants.ON_UPDATED_MISSION, _local2.id));
            };
        }
        public function getTasksWithActionAndType(_arg1:String, _arg2:String):Array{
            var _local5:TaskVO;
            var _local3:Array = this.defaultMission.tasks;
            var _local4:Array = [];
            for each (_local5 in _local3) {
                if ((((_local5.action == _arg1)) && ((_local5.type == _arg2)))){
                    _local4.push(_local5);
                };
            };
            return (_local4);
        }
        public function get levelMissions():Array{
            return (this._levelMissions);
        }
        public function get pendingMissionCreate():Boolean{
            return (this._pendingMissionCreate);
        }
        public function set pendingMissionCreate(_arg1:Boolean):void{
            this._pendingMissionCreate = _arg1;
        }
        public function get allMissions():Array{
            return (this._allMissions);
        }
        public function set allMissions(_arg1:Array):void{
            this._allMissions = _arg1;
        }

    }
}//package net.wooga.pets.models.world 
